<template>
  <div class="flexContent">
    <div class="searchBox">

      <el-form ref="formRef" :model="sForm" class="demo-form-inline" label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="商品名称">
                  <el-input v-clearEmoji v-model.trim="sForm.retrieval" clearable placeholder="商品名称/条码"
                    style="width: 95%;" @keyup.enter.native="searchFn" />
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="门店名称">
                  <el-select v-model="sForm.storeId" popper-class="eloption" style="width: 95%;" filterable
                    placeholder="请选择" @change="changeStore">
                    <el-option v-for="item in brandList" :key="item.storeId" :value="item.storeId"
                      :label="item.storeName" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div class="btnBox">
            <el-button @click.stop="searchFn" class="searchBtn" icon="el-icon-search">搜索</el-button>
            <el-button @click.stop="resetFn" class="resetBtn" icon="el-icon-refresh-right">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>

    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{ resizable: true }" border class="tablebox" ref="xTable1"
          :row-config="{ isHover: true }" :scroll-y="{ enabled: true, gt: 21 }" style="width: 100%" height="98%"
          size="medium" align="center">
          <vxe-column type="seq" width="80" title="序号" />
          <vxe-column field="storeName" title="门店名称" :min-width="elTableAdaptationWidth('storeName', accountList)" />
          <vxe-column field="commodityName" title="商品名称"
            :min-width="elTableAdaptationWidth('commodityName', accountList)" />
          <vxe-column field="barCode" title="商品条码" width="140" />
          <vxe-column field="salesNum" title="销量" min-width="150">
            <template #header="{ column }">
              <div style="display: flex;justify-content: center;">
                <span>销量</span>
                <span style="display: flex;flex-direction: column;">
                  <i style="cursor: pointer;" class="el-icon-caret-top iconCls"
                    :class="sort === '4' ? 'iconClsActived' : ''" @click="sortFn('4')"></i>
                  <i style="cursor: pointer;transform: translateY(-5px);" :class="sort === '3' ? 'iconClsActived' : ''"
                    class="el-icon-caret-bottom iconCls" @click="sortFn('3')"></i>
                </span>
              </div>
            </template>
          </vxe-column>
          <vxe-column field="salesVolume" title="销售额" min-width="150">
            <template #header="{ column }">
              <div style="display: flex;justify-content: center;">
                <span>销售额</span>
                <span style="display: flex;flex-direction: column;">
                  <i style="cursor: pointer;" class="el-icon-caret-top iconCls"
                    :class="sort === '6' ? 'iconClsActived' : ''" @click="sortFn('6')"></i>
                  <i style="cursor: pointer;transform: translateY(-5px);" :class="sort === '5' ? 'iconClsActived' : ''"
                    class="el-icon-caret-bottom iconCls" @click="sortFn('5')"></i>
                </span>
              </div>
            </template>
          </vxe-column>
          <vxe-column field="stockNum" title="库存数量" min-width="120">
            <template #header="{ column }">
              <div style="display: flex;justify-content: center;">
                <span>库存数量</span>
                <span style="display: flex;flex-direction: column;">
                  <i style="cursor: pointer;" class="el-icon-caret-top iconCls"
                    :class="sort === '2' ? 'iconClsActived' : ''" @click="sortFn('2')"></i>
                  <i style="cursor: pointer;transform: translateY(-5px);" :class="sort === '1' ? 'iconClsActived' : ''"
                    class="el-icon-caret-bottom iconCls" @click="sortFn('1')"></i>
                </span>
              </div>
            </template>
          </vxe-column>
          <vxe-column field="initialInventory" title="期初库存数量" width="150" min-width="150">
            <template #header="{ column }">
              <div style="display: flex;justify-content: center;">
                <span>期初库存数量</span>
                <span style="display: flex;flex-direction: column;">
                  <i style="cursor: pointer;" :class="sort === '8' ? 'iconClsActived' : ''"
                    class="el-icon-caret-top iconCls" @click="sortFn('8')"></i>
                  <i style="cursor: pointer;transform: translateY(-5px);" :class="sort === '7' ? 'iconClsActived' : ''"
                    class="el-icon-caret-bottom iconCls" @click="sortFn('7')"></i>
                </span>
              </div>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div>
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,->, sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>

<script>
import { getGoodsList, getShopListSelect } from '@/api/store'
export default {
  name: "IndependentAccount",
  data() {
    return {
      dialogTableVisible: false,
      sForm: {
        storeId: "",
        retrieval: ''
      },
      brandList: [],
      accountList: [],
      sort: '1',
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
      date: [],
      dialogArr: [],
      // {
      //   orderAmount: 0,
      //   profitAmount: 0,
      //   bookingTime: "",
      //   divideCommodityVoList: [],
      // }
      isSearch: false,
    };
  },
  computed: {
    startTime() {
      if (this.date == null) {
        return "";
      }
      if (this.date.length > 0) {
        return this.date[0];
      } else {
        return "";
      }
    },
    endTime() {
      if (this.date == null) {
        return "";
      }
      if (this.date.length > 0) {
        return this.date[1];
      } else {
        return "";
      }
    },
  },
  created() {
    this.getShopEasy();
  },
  methods: {
    // 获取门店列表
    async getShopEasy() {
      const res = await getShopListSelect();
      if (res.code === 200) {
        this.brandList = res.data;
        this.sForm.storeId = this.brandList[0]?.storeId;
        console.log('this.sForm.storeId', this.sForm.storeId);
        if (this.brandList.length > 0) {
          this.getGoodsList();
        }
      }
    },
    // 查找列表数据
    async getGoodsList() {
      const { startTime, endTime } = this;
      if (!this.isSearch) {
        this.sForm.retrieval = "";
      }
      const res = await getGoodsList({
        ...this.page,
        ...this.sForm,
        filter: this.sort
      });
      if (res.code === 200) {
        console.log(res.data, "data");
        this.accountList = res.data.list;
        this.page.total = res.data.total;
        const $table = this.$refs.xTable1
        $table.loadData(this.accountList);
      }
    },
    sortFn(val) {
      console.log('val', val);
      this.sort = val
      this.getGoodsList();
    },
    // 搜索方法
    async searchFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.isSearch = true;
      this.getGoodsList();
    },
    // 重置
    resetFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.isSearch = false;
      this.sForm.retrieval = "";
      this.getGoodsList();
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;

      this.getGoodsList();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      this.page.pageNum = val;
      this.getGoodsList();
    },
    changeStore() {
      this.page.pageNum = 1;
      this.getGoodsList();
    }
  },
};
</script>

<style scoped lang="scss">
.iconCls:hover {
  color: #ee2b08 !important;
}

.iconClsActived {
  color: #ee2b08 !important;
}

.inner {
  .item {
    p {
      display: flex;

      &:nth-child(1) {
        justify-content: space-between;
      }
    }
  }
}
</style>
